package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.AppVariable;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.ArticleInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @author vivi
 * @description
 * @date 2024/6/2
 */
@RequestMapping("/art")
@RestController // 不返回静态页面, 返回数据
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request) {
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        List<ArticleInfo> list = articleService.getMyList(userInfo.getId());
        return AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(Integer id, HttpServletRequest request) {
        if (id == null || id <= 0) {
            // 参数有误
            return AjaxResult.fail(-1, "参数异常");
        }
        UserInfo user = UserSessionUtils.getSessionUser(request);
        if (user == null) {
            return AjaxResult.fail(-2, "用户未登录");
        }
        return AjaxResult.success(articleService.del(id, user.getId()));
    }

    @RequestMapping("/detail")
    public AjaxResult getDetail(Integer id) {
        if (id == null || id <= 0) {
            return AjaxResult.fail(-1, "非法参数");
        }
        return AjaxResult.success(articleService.getDetail(id));
    }

    @RequestMapping("/incr-rcount")
    public AjaxResult incrRCount(Integer id) {
        if (id != null && id > 0) {
            return AjaxResult.success(articleService.incrRCount(id));
        }
        return AjaxResult.fail(-1, "未知错误");
    }

    @RequestMapping("/add")
    public AjaxResult add(ArticleInfo articleInfo, HttpServletRequest request) {
        // 1 非空校验
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle()) ||
                !StringUtils.hasLength(articleInfo.getContent())) {
            return AjaxResult.fail(-1, "非法请求");
        }
        // 2 数据库添加操作
        // 得到当前用户的 uid
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null || userInfo.getId() <= 0) {
            // 无效的登录用户
            return AjaxResult.fail(-2, "无效的登录用户");
        }
        articleInfo.setUid(userInfo.getId());
        return AjaxResult.success(articleService.add(articleInfo));
    }

    @RequestMapping("/update")
    public AjaxResult update(ArticleInfo articleInfo, HttpServletRequest request) {
        // 1 非空校验
        if (articleInfo == null || !StringUtils.hasLength(articleInfo.getTitle()) ||
                !StringUtils.hasLength(articleInfo.getContent()) ||
                articleInfo.getId() == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        // 得到当前登录用户 id
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null || userInfo.getId() == null) {
            // 无效用户
            return AjaxResult.fail(-2, "无效用户");
        }
        articleInfo.setUid(userInfo.getId());
        articleInfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleInfo));
    }

    @RequestMapping("/listbypage")
    //                                      当前页码          每页显示条数
    public AjaxResult getListByPage(Integer pindex, Integer psize) {
        // 1 参数校正
        if (pindex == null || pindex <= 1) {
            pindex = 1;
        }
        if (psize == null || psize <= 1) {
            psize = 2;
        }
        // 2 分页公式的值 = （当前页码-1）*每页显示条数
        int offset = (pindex - 1) * psize;
        List<ArticleInfo> list = articleService.getListByPage(pindex, offset);
        // 当前列表总共有多少页
        // a.总共有多少条数据
        int totalCount = articleService.getCount();
        // b.总条数/psize（每页显示条数）
        double pcountdb = totalCount / (psize * 1.0);
        // c.使用进一法得到总页数
        int pcount = (int) Math.ceil(pcountdb);
        HashMap<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("pcount", pcount);
        return AjaxResult.success(result);
    }
}



















